<template>
<div>
<van-coupon-cell
  :coupons="coupons"
  :chosen-coupon="chosenCoupon"
  @click="showList = true"
/>
<!-- 优惠券列表 -->
<van-popup
  v-model="showList"
  round
  position="bottom"
  style="height: 90%; padding-top: 4px;"
>
  <van-coupon-list
    :coupons="coupons"
    :chosen-coupon="chosenCoupon"
    :disabled-coupons="disabledCoupons"
    @change="onChange"
    @exchange="onExchange"
  />
</van-popup>
</div>
</template>

<script>
export default {
  props:['info_id','more','i'],
  data() {
    return {
      chosenCoupon: -1,
      coupons: [],
      disabledCoupons: [],
      showList:false,
      recindex:''
    };
  },
  methods: {
    onChange(index) {
      this.showList = false;
      this.chosenCoupon = index;
      if(this.more){
      if(index==-1){
         this.$parent.jian_pricearr[this.i]=0;
         this.$parent.ucp_idarr[this.i]=0;
         this.$parent.getData();
         return;
      }
      this.$parent.jian_pricearr[this.i]=this.coupons[index].valueDesc;
      this.$parent.ucp_idarr[this.i]=this.coupons[index].ucp_id;
      }else{
      if(index==-1){
         this.$parent.jian_price=0;
         this.$parent.ucp_id=0;
         this.$parent.getData();
         return;
      }
      this.$parent.jian_price=this.coupons[index].valueDesc;
      this.$parent.ucp_id=this.coupons[index].ucp_id;
      }
      this.$parent.getData();
    },
   async onExchange(code) {
      let res =await this.$frontaxios.post(this.$front.changeDhm,{dhm:code});
            if(res.code==1){
                this.$toast.success('兑换优惠券成功!');
                location.reload();
            }else{
                this.$toast.fail(res.msg);
            }
    },
    async getDate(){
     let res=await this.$frontaxios.post(this.$front.getUserCouponList,{info_id:this.info_id});
     let res2=await this.$frontaxios.post(this.$front.getUserNoCouponList,{info_id:this.info_id});
     if(res.code==1){
       this.coupons=res.data.list;
     }
     if(res2.code==1){
       this.disabledCoupons=res2.data.list;
     }
    }
  },
  created(){
       this.getDate();
  }
};
</script>